﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace UDPTest
{
    public enum HasCode { ASCII, UNICODE, UTF_8, UTF_7, }
    /// <summary>
    /// 编码
    /// </summary>
    public class Decoding
    {
        //Decoding 算法 //16进制 HEX 编码出文本的算法
        public string get_Hextext(HasCode code, string text)
        {
            string str = null;
            if (text == null) { return str; }
            string pattern = "[^a-fA-F0-9]";
            Regex regex = new Regex(pattern);
            if (!regex.Match(text).Success)
            {
                byte[] bytes = new byte[0x1000];
                int length = text.Length;
                int num2 = 0;
                if ((length % 2) == 1)
                { length--; text = text.Substring(0, length); }
                length /= 2;
                for (int i = 0; i < length; i++)
                {
                    num2 = (int)long.Parse(text.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier);
                    bytes[i] = (byte)num2;
                }
                switch (code)
                {
                    case HasCode.ASCII: return Encoding.Default.GetString(bytes, 0, length);
                    case HasCode.UNICODE: return Encoding.Unicode.GetString(bytes, 0, length);
                    case HasCode.UTF_8: return Encoding.UTF8.GetString(bytes, 0, length);
                    case HasCode.UTF_7: return Encoding.UTF7.GetString(bytes, 0, length);
                }
                return str;
            }
            return null;
        }

        //Encoding //算法文本转换成各种编码(16进制)的算法
        public string Convert(string inputText, HasCode code)
        {
            if (inputText != null)
            {
                switch (code)
                {
                    case HasCode.ASCII:
                        {
                            StringBuilder builder = new StringBuilder();
                            byte[] bytes = Encoding.Default.GetBytes(inputText);
                            for (int i = 0; i < bytes.Length; i++)
                            { builder = builder.Append(bytes[i].ToString("X2")); }
                            return builder.ToString();
                        }
                    case HasCode.UNICODE:
                        {
                            StringBuilder builder2 = new StringBuilder();
                            byte[] buffer2 = Encoding.Unicode.GetBytes(inputText);
                            for (int j = 0; j < buffer2.Length; j++)
                            { builder2 = builder2.Append(buffer2[j].ToString("X2")); }
                            return builder2.ToString();
                        }
                    case HasCode.UTF_8:
                        {
                            StringBuilder builder3 = new StringBuilder();
                            byte[] buffer3 = Encoding.UTF8.GetBytes(inputText); for (int k = 0; k < buffer3.Length; k++)
                            { builder3 = builder3.Append(buffer3[k].ToString("X2")); }
                            return builder3.ToString();
                        }
                    case HasCode.UTF_7:
                        {
                            StringBuilder builder4 = new StringBuilder();
                            byte[] buffer4 = Encoding.UTF7.GetBytes(inputText);
                            for (int m = 0; m < buffer4.Length; m++)
                            { builder4 = builder4.Append(buffer4[m].ToString("X2")); }
                            return builder4.ToString();
                        }
                }
            } return null;
        }

        //二进制转字符串：   
        public string ConvertToString(byte[] thebyte)
        {
            char[] Chars = new char[System.Text.Encoding.Default.GetCharCount(thebyte, 0, thebyte.Length)];
            System.Text.Encoding.Default.GetChars(thebyte, 0, thebyte.Length, Chars, 0);
            string newString = new string(Chars);
            return newString;
        }

        //字符串转二进制：   
        public byte[] ConvertToByte(string theString)
        {
            byte[] byteStream = System.Text.Encoding.Default.GetBytes(theString);
            return byteStream;
        }
    }
}